package com.snow.useviewdemo.play;

import android.content.Context;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.VideoView;

import com.bumptech.glide.Glide;
import com.snow.playl.interfaze.VideoPlayStateListenerHelper;
import com.snow.playl.manager.PlayVideoManager;
import com.snow.playl.view.BaseVideoPlayView;
import com.snow.useviewdemo.R;
import com.snow.useviewdemo.util.TextViewUtils;

import static com.snow.useviewdemo.play.NomalPlayVideoAct.TEST_VIDEO;

/**
 * Created by zhaoyingtao
 * Date: 2020/7/16
 * Describe:
 */
public class FloatWindowManager {
    private volatile static FloatWindowManager windowManager;

    public static FloatWindowManager init() {
        if (windowManager == null) {
            synchronized (FloatWindowManager.class) {
                if (windowManager == null) {
                    windowManager = new FloatWindowManager();
                }
            }
        }
        return windowManager;
    }


    public void showFloatingWindowView(Context context) {
        if (context == null) {
            return;
        }
        // 获取系统窗口管理服务
        WindowManager mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        View view = initFloatView(context, mWindowManager);
        // 悬浮窗口参数设置及返回
        WindowManager.LayoutParams mFloatParams = getParams();
        // 设置窗口触摸移动事件
        view.setOnTouchListener(new FloatViewMoveListener(mWindowManager, view, mFloatParams));
        // 悬浮窗生成
        mWindowManager.addView(view, mFloatParams);
    }

    private View initFloatView(Context mContext, WindowManager mWindowManager) {
        View view = View.inflate(mContext, R.layout.float_play_video, null);
        // 设置视频封面
        BaseVideoPlayView playView = view.findViewById(R.id.video_play_view);
        // 悬浮窗关闭
        view.findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mWindowManager != null) {
                    mWindowManager.removeView(view);
                }
            }
        });
//        playView.playVideo(TEST_VIDEO);
//         PlayVideoManager.init().playVideo(TEST_VIDEO);
        PlayVideoManager.init().setPlayVideoStateListener(new VideoPlayStateListenerHelper() {
            @Override
            public void startPlayMusic() {
                playView.hindVideoCoverLoading();
            }
        });
        return view;
    }

    public WindowManager.LayoutParams getParams() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        //设置悬浮窗口类型
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        //设置悬浮窗口属性
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        //设置悬浮窗口透明
        layoutParams.format = PixelFormat.TRANSLUCENT;
        //设置悬浮窗口长宽数据
        layoutParams.width = 600;
        layoutParams.height = 340;
        //设置悬浮窗显示位置
        layoutParams.gravity = Gravity.START | Gravity.TOP;
        layoutParams.x = 100;
        layoutParams.y = 100;
        return layoutParams;
    }
}
